package com.ojoin.ojoin.eb_spider.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.StatTotalService;
import com.ojoin.ojoin.eb_spider.service.stat.StatTotal;
import com.ojoin.ojoin.eb_spider.utils.SpringUtilsSpider;
import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.URLUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformRule;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
@Service
public class StatTotalServiceImpl implements StatTotalService {

	private Logger log = LoggerFactory.getLogger(StatTotalServiceImpl.class);

	@Override
	public TbSlLoanStatTotal statTotal(int platformUserId) {

		TbSlLoanStatTotal loanStatTotal = null;
		try {
			TbSlPlatformUser user = WebServiceUtils
					.getPlatformUser(platformUserId);

			loanStatTotal = stat(user);

		} catch (Exception e) {
			LogUtils.error(log, e);
		}

		return loanStatTotal;
	}

	@Override
	public TbSlLoanStatTotal stat(TbSlPlatformUser user) throws Exception {

		String suffix = "stat";

		TbSlLoanStatTotal total = new TbSlLoanStatTotal();
		total.setIdOfPlatformUser(user.getId());
		total.setIdOfTbMerchantUser(user.getIdOfTbMerchantUser());
		total.setPlatformName(user.getPlatformName());
		total.setStatEndDate(TimeUtils.getCurDate());

		String className = user.getPlatformName().replaceAll("_", "");

		if (!CommonUtils.isEmpty(user.getPlatformRuleId())) {
			TbSlPlatformRule rule = WebServiceUtils.queryplatformRule(user.getPlatformRuleId());
			if (!CommonUtils.isEmpty(rule.getRuleClass())) {
				className = rule.getRuleClass();
			}

		}
		
		TbCatchdataStrategy ca = WebServiceUtils.getCatchdataStrategyByUserId(user.getId());
		
		StatTotal service = SpringUtilsSpider.getBeanByClassName(className
				+ suffix);

		
		service.invoke(user, ca,total);

		LogUtils.info(log, "-->" + JSONUtils.toJSONString(total));
		return total;

	}

	@Override
	public void invoke(TbSlPlatformUser user,TbCatchdataStrategy ca, TbSlLoanStatTotal total)
			throws Exception {

		String stime = WebServiceUtils.getPaymentLastsettlementDate(
				user.getId()).replace("/", "-");

		if (CommonUtils.isEmpty(stime)) {
			stime = WebServiceUtils.getPaymentLastInsertTime(user.getId());
		}

		String etime = TimeUtils.getCurDate();

		long inveInMoney = WebServiceUtils
				.getSupplyInventoryMoneyByplateformId(user.getId(),ca, stime,
						etime, TbSlSupplyInventory.IN);

		long inveOutMoney = WebServiceUtils
				.getSupplyInventoryMoneyByplateformId(user.getId(),ca, stime,
						etime, TbSlSupplyInventory.OUT);

		long paymoney = WebServiceUtils.getSupplyPaymentMoneyByplateformId(
				user.getId(), ca,stime, etime, null);

		URLUtils.getJsonByUrl(WebServiceUtils.basePaymentUrl
				+ "?platformAccountId=" + user.getId());

		total.setTotalUnsettleInstoreorder(Long.valueOf(inveInMoney));
		total.setTotalUnenterinRefundorder(Long.valueOf(inveOutMoney));
		total.setTotalUnpaySettleorder(Long.valueOf(paymoney));

		LogUtils.info(
				log,
				"%s -->  %s-->[ store all in %s ] + [all Unpay] [settlementDate %s]",
				user.getUsername(), inveInMoney, stime);

	}

}
